package winOrDie.GameEngine.RT;

import java.util.Iterator;
import java.util.PriorityQueue;

import winOrDie.GameEngine.RT.mp.MapBoard;


public class Bucket 
{

	private PriorityQueue<WoD_Event> bucket = null;

	private long rowLength = MapBoard.FPS;
	public int lagFactor = 5;

	private WoD_Event_Processor processor = null;

	public Bucket(WoD_Event_Processor processor) {
		this.processor = processor;
		bucket = new PriorityQueue<WoD_Event>();
	}


	public void addEvent(WoD_Event event)
	{
		
		event.setTs(event.getTs() + (lagFactor * rowLength));
		synchronized (bucket) {
			bucket.add(event);
		}
		
	}


	public void processRoundEvent(long time)
	{
		synchronized (bucket) {
			Iterator<WoD_Event> it = bucket.iterator();

			boolean finished = false;
			long tolerance = 150L;

			while ((!finished) && (it.hasNext())) {
				WoD_Event event = it.next();
				if (event.getTs() + tolerance < time)
				{
					System.out.println("TARDE " + time + " " + event.getTs() + " user " + event.getPlayer());
					System.out.println("Difference " + Long.valueOf(time - event.getTs()));
					it.remove();
				}
				else if (event.getTs() < time + rowLength)
				{
					processor.processEvent(event);
					it.remove();
				}
				else if (event.getTs() >= time + rowLength)
				{
					finished = true;
				}
				
			}
		}
		
	}

}
